﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CommonLib
{
    #region StringHandle
    /// <summary>
    /// 文典字符串處理類StringHandle
    /// lancy@2007-2-2
    /// </summary>
    public class StringHandle
    {
        /// <summary>
        /// 靜態方法:截取字符串長度
        /// </summary>
        /// <param name="inputString">要截取的字符串</param>
        /// <param name="len">結果字符串的字節數</param>
        /// <returns>返回截取後的字符串,如果長度大於len則截取後加...如果小於等於len則顯示全部</returns>
        public static string CutString(string inputString, int len)
        {
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }
                if (tempLen > len)
                    break;
            }
            //如果截过则加上半个省略號 
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            if (mybyte.Length > len)
                tempString += "…";


            return tempString;
        }
        /// <summary>
        /// 靜態方法:給指定字符串加改變色調
        /// </summary>
        /// <param name="str">要改變色調的字符串</param>
        /// <param name="ColorString">色值</param>
        /// <returns></returns>
        public static string StringAddCorlor(string str, string ColorString)
        {
            str = "<font color=" + ColorString + ">" + str + "</font>";
            return str;
        }
        /// <summary>
        /// 刪除AStr字串中ASeparate的分隔字串
        /// </summary>
        /// <param name="AStr">字串參數</param>
        /// <param name="ASeparate">分隔符號</param>
        /// <returns>已清除ASeparate之字串,若AStr=null則回傳null, AStr=""則回傳""</returns>
        public static string CancelSeparateLine(string AStr, string ASeparate)
        {
            if (AStr == null)
                return null;
            if (AStr.Length == 0)
                return "";
            string sTemp = AStr;
            for (int i = 0; i < sTemp.Length; i++)
            {
                if (sTemp.IndexOf(ASeparate) != -1)
                {
                    sTemp = sTemp.Remove((sTemp.IndexOf(ASeparate)), ASeparate.Length);
                }
                else
                    break;
            }
            return sTemp;
        }

        /// <summary>
        /// 在數字前填滿n位數的零，並轉為字串格式
        /// </summary>
        /// <param name="i">欲轉換之數字</param>
        /// <param name="Number">欲填滿之總位數</param>
        /// <returns>回傳已進行轉換之字串</returns>
        public static string IntToStrFillZero(int i, int Number)
        {
            string s = Convert.ToString(i);
            while (s.Length < Number)
            {
                s = "0" + s;
            }
            return s;
        }

        /**/
        /// <summary> 
        /// 过滤sql中非法字符 
        /// </summary> 
        /// <param name="value">要过滤的字符串 </param> 
        /// <returns>string </returns> 
        public static string Filter(string value)
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;
            //value = Regex.Replace(value, @";", string.Empty);
            value = Regex.Replace(value, @"'", string.Empty);
            //value = Regex.Replace(value, @"&", string.Empty);
            value = Regex.Replace(value, @"%20", string.Empty);
            value = Regex.Replace(value, @"--", string.Empty);
            value = Regex.Replace(value, @"==", string.Empty);
            value = Regex.Replace(value, @"<", string.Empty);
            value = Regex.Replace(value, @">", string.Empty);
            value = Regex.Replace(value, @"%", string.Empty);

            return value;
        }


    }
    #endregion
}
